package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UsertextService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//用户控制器

@Slf4j
@RestController
@RequestMapping("/usertext")
public class UsertextController {

    //依赖注入, 通过Dubbo的远程调用, 把Zookeeper中的UsertextService实现类对象进行 赋值usertxetService
    @Reference
    private UsertextService usertextService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            //调用service中的分页查询方法
            PageResult pageResult = usertextService.pageQuery(queryPageBean);
            //查询成功, 返回查询成功的信息 及 数据
            return pageResult;
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败, 返回查询失败的信息 及 数据
            return new PageResult(0L, null);
        }
    }


    //删除用户信息
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            //调用service中的删除方法
            usertextService.deleteById(id);
            //删除成功, 返回删除成功的信息
            return new Result(true, "删除用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除用户失败");
        }
    }



    //更新用户
    @RequestMapping("/edit")
    public Result edit(@RequestBody User user, Integer[] roleIds) {
        try {
            //调用service中的更新方法
            usertextService.edit(user, roleIds);
            //更新成功, 返回更新成功的信息
            return new Result(true, "更新用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            //更新失败, 返回更新失败的信息
            return new Result(false, "更新用户失败");
        }
    }

    //添加用户
    @RequestMapping("/add")
    public Result add(@RequestBody User user, Integer[] roleIds) {
        try {
            //调用service中的添加方法
            usertextService.add(user, roleIds);
            //添加成功, 返回添加成功的信息
            return new Result(true, "添加用户成功");
        } catch (Exception e) {
            e.printStackTrace();
            //添加失败, 返回添加失败的信息
            return new Result(false, "添加用户失败");
        }
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            User user = usertextService.findById(id);
            return new Result(true, MessageConst.ACTION_SUCCESS,user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }

    @RequestMapping("/findroleIdsByUserId")
    public Result findroleIdsByUserId(Integer id){
        try {
            List<Integer> list = usertextService.findroleIdsByUserId(id);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>"+list);
            return new Result(true, MessageConst.ACTION_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}